package com.sduept.nwld.primaryequipment.rest;

import com.alibaba.fastjson.JSONObject;
import com.sduept.core.dao.SuperDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/audit/")
public class ProjectAuditService {
	@Autowired
	private SuperDAO dao;

	@Resource
	private DataSource serverDatasource;
	

	@GetMapping("/getProjectAudit")
	public String findAllPages(){
		JSONObject obj = new JSONObject();
		List<Object> countList = new ArrayList<Object>();
		
		PreparedStatement st = null;
		Connection connect = null;
		ResultSet res = null;
		String sql = "select page,sum(visitCount)num from t_org_audit where code!='admin' group by url order by num desc";
		try {
			connect = serverDatasource.getConnection();
			st = connect.prepareStatement(sql);
			res = st.executeQuery(sql);
			while(res.next()){
				String page = res.getString("page");
				Integer count = res.getInt("num");
				Map<String, Object> countMap = new HashMap<String, Object>();
				countMap.put("name", page);
				countMap.put("value", count);
				countList.add(countMap);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				res.close();
				connect.close();
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return obj.toJSONString(countList);
	}
	

	@GetMapping("/getVisitorByPage/{selectedPage}")
	public String findVisitorByPage(@PathVariable("selectedPage") String selectedPage) {
		//传过来的参数有双引号，去掉双引号
		String selected = selectedPage.replace("\"", "");
		JSONObject obj = new JSONObject();
		List<Object> countList = new ArrayList<Object>();
		
		PreparedStatement st = null;
		Connection connect = null;
		ResultSet res = null;
		String sql = "select name,sum(visitCount) visitCount from t_org_audit where page = '"+selected+"' and code !='admin' group by code order by visitCount desc";
		try {
			connect = serverDatasource.getConnection();
			st = connect.prepareStatement(sql);
			res = st.executeQuery(sql);
			while(res.next()){
				String name = res.getString("name");
				Integer count = res.getInt("visitCount");
				Map<String, Object> countMap = new HashMap<String, Object>();
				countMap.put("visitor", name);
				countMap.put("value", count);
				countList.add(countMap);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				res.close();
				connect.close();
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return obj.toJSONString(countList);
	}
	

	@GetMapping("/getProjectAuditLoginTime")
	public String getProjectAuditLoginTime(){
		JSONObject obj = new JSONObject();
		List<Object> countList = new ArrayList<Object>();
		
		PreparedStatement st = null;
		Connection connect = null;
		ResultSet res = null;
		String sql = "select name, loginTime from t_logintime where code!='admin' order by loginTime desc";
		//String sql = "select name, loginTime from t_logintime order by loginTime desc";
		try {
			connect = serverDatasource.getConnection();
			st = connect.prepareStatement(sql);
			res = st.executeQuery(sql);
			while(res.next()){
				String name = res.getString("name");
				Integer count = res.getInt("loginTime");
				Map<String, Object> countMap = new HashMap<String, Object>();
				countMap.put("name", name);
				countMap.put("loginTime", count);
				countList.add(countMap);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				res.close();
				connect.close();
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return obj.toJSONString(countList);
	}
}
